import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-06-10
 * Time: 14:55
 */
//这个类用来实现动态生成主页面
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //此处禁止创建会话,如果没找到,认为用户是未登录的状态
        //如果找到了,才认为是登录状态
        HttpSession session=req.getSession(false);
        if(session==null){
            //当前未登录
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户未登录");
            return;
        }
        String  username=(String)session.getAttribute("username");
        //虽然有会话对象,但是没有属性,也认为是登录状态异常
        if(username==null){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户未登录");
            return;
        }

        //如果都没问题,生成动态页面
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎你!"+username);
    }

}
